<template>
	<view class="container">
		<view class="form-box">

			<view class="form-item">
				<input class="username" :value="username" @input="bindUsernameInput" placeholder="用户名" auto-focus />
				<image v-if=" username.length > 0 " id="clear-username" class="clear" src="/static/images/clear_input.png"
				 @tap.stop="clearInput"></image>
			</view>

			<view class="form-item">
				<input class="password" :value="password" password @input="bindPasswordInput" placeholder="密码" />
				<image class="clear" id="clear-password" v-if="password.length > 0 " src="/static/images/clear_input.png" @tap.stop="clearInput"></image>
			</view>

			<view class="form-item">
				<input class="password" :value="confirmPassword" password @input="bindConfirmPasswordInput" placeholder="确认密码" />
				<image class="clear" id="clear-confirm-password" v-if="confirmPassword.length > 0 " src="/static/images/clear_input.png"
				 @tap.stop="clearInput"></image>
			</view>

			<view class="form-item">
				<input class="mobile" :value="mobile" @input="bindMobileInput" placeholder="手机号" />
				<image v-if="mobile.length > 0 " id="clear-mobile" class="clear" src="/static/images/clear_input.png" @tap.stop="clearInput"></image>
			</view>

			<view class="form-item-code">
				<view class="form-item code-item">
					<input class="code" :value="code" @input="bindCodeInput" placeholder="验证码" />
					<image class="clear" id="clear-code" v-if=" code.length > 0 " src="/static/images/clear_input.png" @tap.stop="clearInput"></image>
				</view>
				<view class="code-btn" @tap="sendCode">获取验证码</view>
			</view>

			<button type="primary" class="register-btn" @tap="startRegister">注册</button>

		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				username: '',
				password: '',
				confirmPassword: '',
				mobile: '',
				code: ''
			}
		},
		methods: {
			...mapMutations(['setCode', 'login']),
			sendCode: function() {
				let that = this;
				if (this.mobile.length == 0) {
					uni.showModal({
						title: '错误信息',
						content: '手机号不能为空',
						showCancel: false
					});
					return false;
				}

				if (!this.$check.isValidPhone(this.mobile)) {
					uni.showModal({
						title: '错误信息',
						content: '手机号输入不正确',
						showCancel: false
					});
					return false;
				}

				uni.request({
					url: that.$api.api.AuthRegisterCaptcha,
					data: {
						mobile: that.mobile
					},
					method: 'POST',
					header: {
						'content-type': 'application/json'
					},
					success: function(res) {
						if (res.data.errno == 0) {
							uni.showModal({
								title: '发送成功',
								content: '验证码已发送',
								showCancel: false
							});
						} else {
							uni.showModal({
								title: '错误信息',
								content: res.data.errmsg,
								showCancel: false
							});
						}
					}
				});
			},
			requestRegister: function(wxCode) {
				let that = this;
				uni.request({
					url: that.$api.api.AuthRegister,
					data: {
						username: that.username,
						password: that.password,
						mobile: that.mobile,
						code: that.code,
						wxCode: wxCode
					},
					method: 'POST',
					header: {
						'content-type': 'application/json'
					},
					success: function(res) {
						if (res.data.errno == 0) {
							// app.globalData.hasLogin = true;
							// wx.setStorageSync('userInfo', res.data.data.userInfo);
							that.login(res.data.data.userInfo);
							uni.setStorage({
								key: "token",
								data: res.data.data.token,
								success: function() {
									uni.switchTab({
										url: '/pages/ucenter/index/index'
									});
								}
							});
						} else {
							uni.showModal({
								title: '错误信息',
								content: res.data.errmsg,
								showCancel: false
							});
						}
					}
				});
			},
			startRegister: function() {
				var that = this;

				if (this.password.length < 6 || this.username.length < 6) {
					uni.showModal({
						title: '错误信息',
						content: '用户名和密码不得少于6位',
						showCancel: false
					});
					return false;
				}

				if (this.password != this.confirmPassword) {
					uni.showModal({
						title: '错误信息',
						content: '确认密码不一致',
						showCancel: false
					});
					return false;
				}

				if (this.mobile.length == 0 || this.code.length == 0) {
					uni.showModal({
						title: '错误信息',
						content: '手机号和验证码不能为空',
						showCancel: false
					});
					return false;
				}

				if (!that.$check.isValidPhone(this.mobile)) {
					uni.showModal({
						title: '错误信息',
						content: '手机号输入不正确',
						showCancel: false
					});
					return false;
				}

				uni.login({
					success: function(res) {
						if (!res.code) {
							uni.showModal({
								title: '错误信息',
								content: '注册失败',
								showCancel: false
							});
						}

						that.requestRegister(res.code);
					}
				});
			},
			bindUsernameInput: function(e) {

				this.username= e.detail.value;
			},
			bindPasswordInput: function(e) {

				this.password= e.detail.value;
			},
			bindConfirmPasswordInput: function(e) {

				this.confirmPassword= e.detail.value;
			},
			bindMobileInput: function(e) {

				this.mobile= e.detail.value;
			},
			bindCodeInput: function(e) {

				this.code= e.detail.value;
			},
			clearInput: function(e) {
				switch (e.currentTarget.id) {
					case 'clear-username':
						this.username= '';
						break;
					case 'clear-password':
						this.password= '';
						break;
					case 'clear-confirm-password':
						this.confirmPassword='';
						break;
					case 'clear-mobile':
						this.mobile='';
						break;
					case 'clear-code':
						this.code= '';
						break;
				}
			}
		}
	}
</script>

<style>
	.form-box {
		width: 100%;
		height: auto;
		overflow: hidden;
		padding: 0 40rpx;
		margin-top: 96rpx;
		background: #fff;
	}

	.form-item {
		position: relative;
		background: #fff;
		height: 96rpx;
		border-bottom: 1px solid #d9d9d9;
	}

	.form-item .username,
	.form-item .password,
	.form-item .mobile,
	.form-item .code {
		position: absolute;
		top: 26rpx;
		left: 0;
		display: block;
		width: 100%;
		height: 44rpx;
		background: #fff;
		color: #333;
		font-size: 30rpx;
	}

	.form-item-code {
		margin-top: 32rpx;
		height: auto;
		overflow: hidden;
		width: 100%;
	}

	.form-item-code .form-item {
		float: left;
		width: 350rpx;
	}

	.form-item-code .code-btn {
		float: right;
		padding: 20rpx 40rpx;
		border: 1px solid #d9d9d9;
		border-radius: 10rpx;
		color: #fff;
		background: green;
	}

	.form-item .clear {
		position: absolute;
		top: 26rpx;
		right: 18rpx;
		z-index: 2;
		display: block;
		background: #fff;
		height: 44rpx;
		width: 44rpx;
	}

	.register-btn {
		margin: 60rpx 0 40rpx 0;
		height: 96rpx;
		line-height: 96rpx;
		color: #fff;
		font-size: 30rpx;
		width: 100%;
		border-radius: 6rpx;
	}
</style>
